package com.example.xuecheng.contentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.xuecheng.contentservice.mapper.CourseTeacherMapper;
import com.example.xuecheng.contentservice.service.CourseTeacherService;
import dto.SaveCourseTeacherDto;
import exception.XueChengException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.CourseTeacher;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/** 课程老师管理接口
 * @author xiehaidong
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Resource
    private CourseTeacherMapper courseTeacherMapper;
    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> courseTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseTeacherLambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseId);
        return courseTeacherMapper.selectList(courseTeacherLambdaQueryWrapper);
    }

    @Override
    public void saveCourseTeacherDto(SaveCourseTeacherDto saveCourseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(saveCourseTeacherDto,courseTeacher);
if(saveCourseTeacherDto.getId()==null){

//    courseTeacher.setCreateDate(LocalDateTime.now());
    int insert = courseTeacherMapper.insert(courseTeacher);
    if(insert<1){
        throw new XueChengException(500,"添加老师失败");
    }
}
else {
    int update = courseTeacherMapper.updateById(courseTeacher);
    if(update<1){
        throw new XueChengException(500,"更新老师失败");
    }
}
    }

    @Override
    public void deleteTeacher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> courseTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseTeacherLambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseId);
        courseTeacherLambdaQueryWrapper.eq(CourseTeacher::getId,id);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(courseTeacherLambdaQueryWrapper);
        if(courseTeacher==null){
            throw new XueChengException(500,"课程教师不存在");
        }
        int delete = courseTeacherMapper.delete(courseTeacherLambdaQueryWrapper);
        if(delete<1){
            throw new XueChengException(500,"删除失败");
        }
    }
}
